package JDk8新时间.LocalDate_LocalTime_LocalDateTime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Test {
    public static void main(String[] args) {
        //认识掌握LocalDate
        // 1. 获取本地日期对象
        LocalDate ld = LocalDate.now();
        System.out.println(ld);

        // 2. 获取日期对象中的信息
        int year = ld.getYear();
        int month = ld.getMonthValue();
        int day = ld.getDayOfMonth();
        int dayOfYear = ld.getDayOfYear();
        int dayOfWeek = ld.getDayOfWeek().getValue();
        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
        System.out.println(dayOfYear);
        System.out.println(dayOfWeek);

        // 3. 直接修改某个信息: withYear, withMonth, withDayOfMonth, withDayOfYear
        LocalDate ld2 = ld.withYear(1900);//在JDK8之前,修改年月日是直接修改,而在此之后,会修改后返回给你,原来值不变
        System.out.println(ld2);
        System.out.println(ld);

        // 4. 把某个信息加多少: plusYears, plusMonths, plusDays, plusWeeks
        LocalDate ld3 = ld.plusDays(2);
        System.out.println(ld3);
        System.out.println(ld);

        // 5. 把某个信息减多少: minusYears, minusMonths, minusDays, minusWeeks
        LocalDate ld4 = ld.minusDays(2);
        System.out.println(ld4);
        System.out.println(ld);

        // 6. 指定日期的LocalDate对象,  LocalDate.of(年,月,日)
        LocalDate ld5 = LocalDate.of(1900,10,16);
        System.out.println(ld5);

        // 7. 判断2个日期是否相等,在前还是在后: equals isBefore isAfter
        System.out.println(ld5.equals(ld4));



        //LocalTime 的用法和LocalDate差不多
        //前缀一样,后面是指时分秒纳秒

        //localDateTime 包括年月日,时分秒,纳秒,也是用法差不多
        //有一点,它可以转换为LocalDate类型和LocalTime类型
        LocalDateTime s = LocalDateTime.now();
        LocalDate s1 = s.toLocalDate();
        LocalTime s2 = s.toLocalTime();
        //也可以把这两个类型合成LocalDateTime类型
        LocalDateTime s3 = LocalDateTime.of(s1,s2);
        System.out.println(s);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

    }
}
